#include "clickedlabel.h"
#include <QMouseEvent>

ClickedLabel::ClickedLabel(QWidget *parent)
    :QLabel(parent)
    ,_curstate(ClickLbState::Normal)
{

}

void ClickedLabel::mousePressEvent(QMouseEvent *event)
{
    qDebug()<<"点击事件触发";
    if(event->button()==Qt::LeftButton){
        if(_curstate == ClickLbState::Normal){
            _curstate = ClickLbState::Selected;
            setProperty("state",_select);
            repolish(this);
            update();
        }else{
            _curstate = ClickLbState::Normal;
            setProperty("state",_normal);
            repolish(this);
            update();
        }
        return;
    }
    QLabel::mousePressEvent(event);
}

void ClickedLabel::enterEvent(QEvent *event)
{
    if(_curstate == ClickLbState::Normal){
        setProperty("state",_normal_hover);
    }else{
        setProperty("state",_select_hover);
    }
    repolish(this);
    update();
    QLabel::enterEvent(event);
}

void ClickedLabel::leaveEvent(QEvent *event)
{
    if(_curstate == ClickLbState::Normal){
        setProperty("state",_normal);
    }else{
        setProperty("state",_select);
    }
    repolish(this);
    update();
    QLabel::leaveEvent(event);
}

void ClickedLabel::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton){
        if(_curstate == ClickLbState::Normal){
            repolish(this);
            update();
        }else{
            repolish(this);
            update();
        }
        emit clicked(this->text(),_curstate);
        return;
    }
    QLabel::mouseReleaseEvent(event);
}

void ClickedLabel::SetState(QString normal, QString hover, QString press, QString select, QString select_hover, QString select_press)
{
    _normal = normal;
    _normal_hover = hover;
    _normal_press = press;
    _select = select;
    _select_hover = select_hover;
    _select_press = select_press;
    setProperty("state",normal);
    _curstate = ClickLbState::Normal;
    repolish(this);
}

ClickLbState ClickedLabel::GetCurState()
{
    return _curstate;
}

bool ClickedLabel::SetCurState(ClickLbState state)
{
    return false;
}

void ClickedLabel::ResetNormalState()
{

}
